package com.congwulin.wechat.controller;

import com.congwulin.wechat.entity.Station;
import com.congwulin.wechat.mapper.StationMapper;
import com.congwulin.wechat.mapper.StationTestMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping(value = "station")
public class StationController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StationTestMapper mapper;

    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    public@ResponseBody
    List<Station> getList(){
        return mapper.list();
    }

    @RequestMapping(value = "/getListByName",method = RequestMethod.GET)
    public@ResponseBody
    List<Station> getListByName(String name){
        return mapper.listByName(name);
    }

    @RequestMapping(value = "/getListSample",method = RequestMethod.GET)
    public@ResponseBody
    List<Station> getListSample(){
        return mapper.listSample();
    }

    @RequestMapping(value = "/getByNameAndCode",method = RequestMethod.GET)
    public@ResponseBody
    Station get(String name,String code){
        return mapper.get(name,code);
    }

    @RequestMapping(value = "/getStationByNameAndCode",method = RequestMethod.GET)
    public@ResponseBody
    Station getStation(String name,String code){
        return mapper.getStation(name,code);
    }

    @RequestMapping(value = "/mybatisConfig",method = RequestMethod.GET)
    public@ResponseBody
    List<Station> mybatisConfig(String name){
        String resource = "mybatis_config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        StationTestMapper mapper = sqlSession.getMapper(StationTestMapper.class);
        StationMapper mapper = sqlSession.getMapper(StationMapper.class);
        return mapper.selectByName("北京");
//        return sqlSession.selectList("listSample");
    }
}
